'''
/******************************************************************
*
* Copyright 2016 Samsung Electronics All Rights Reserved.
*
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************/
'''

print "--------------------------Preparing extlibs ----------------------"

import os


env = Environment()

http_proxy = os.getenv('http_proxy', '')
https_proxy = os.getenv('https_proxy', '')

default_targets = []


if os.path.isfile("gtest-1.7.0/src/gtest_main.cc"):
    print "GTest library exists"
else:
    print "GTest does not exists, starts Downloding..."
    gtest = Command("Gtest", [],
        [
                'pwd',
                'export https_proxy=' + https_proxy + ' && mkdir -p extlibs/tmp && wget http://github.com/google/googletest/archive/release-1.7.0.zip --no-check-certificate',
                'mv release-1.7.0.zip extlibs/tmp/gtest-1.7.0.zip',
                'unzip -o extlibs/tmp/gtest-1.7.0.zip -d extlibs/tmp',
                'mkdir -p extlibs/gtest-1.7.0/src && cp -R extlibs/tmp/googletest-release-1.7.0/src/* extlibs/gtest-1.7.0/src/',
                'mkdir -p extlibs/gtest-1.7.0/include && cp -R extlibs/tmp/googletest-release-1.7.0/include/* extlibs/gtest-1.7.0/include/',
                'rm -rf extlibs/tmp',
                'chmod 777 extlibs/gtest-1.7.0/src/*',
                'chmod 777 -R extlibs/gtest-1.7.0/include/*',
        ])
    default_targets+=gtest

if os.path.isfile("taf/lib/unix/libuv.a"):
    print "Libuv library exists"
else:
    print "Libuv does not exists, starts Downloding..."

    libuv = Command("Libuv", [],
        [
                'pwd',
                'export https_proxy=' + https_proxy + ' && wget http://dist.libuv.org/dist/v1.22.0/libuv-v1.22.0.tar.gz --no-check-certificate',
                'tar -xf libuv-v1.22.0.tar.gz',
                'cd libuv-v1.22.0 && git clone https://chromium.googlesource.com/external/gyp.git build/gyp',
                'cd libuv-v1.22.0 && ./gyp_uv.py -f make',
                'cd libuv-v1.22.0 && BUILDTYPE=Release make -C out',
                'mkdir -p extlibs/taf/include/libuv && cp -r libuv-v1.22.0/include/* extlibs/taf/include/libuv/',
                'mkdir -p extlibs/taf/lib/unix && cp libuv-v1.22.0/out/Release/libuv.a extlibs/taf/lib/unix/',
                'pwd && rm -rf libuv-v1.22.0*',
        ])
    default_targets+=libuv

env.Default(default_targets)
